package share.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import share.security.auth.AuthFactory;
import share.security.auth.Authenticator;

public class Login extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4122442319288790297L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}
	
	//logout
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String referer=request.getHeader("referer");
		Authenticator auth=AuthFactory.newAuthenticator(request.getSession());
		auth.logout();
		response.sendRedirect(referer);
	}

	//login
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String referer=request.getHeader("referer");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		Authenticator auth=AuthFactory.newAuthenticator(request.getSession());
		auth.login(username, password);
		response.sendRedirect(referer);
	}

	/**
	 * Initialization of the servlet. <br>
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
